package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.entity.Administrator;
import com.lovelyj.trip_by_train.entity.Passenger;
import com.lovelyj.trip_by_train.entity.Station;
import com.lovelyj.trip_by_train.service.PassengerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/passenger")
public class PassengerController {

    @Autowired
    private PassengerService passengerService;


    @PutMapping("/create")
    public R<String> create(HttpServletRequest request, @RequestBody Passenger passenger) {
        log.info("开始创建passenger={}", passenger);
        HttpSession session = request.getSession(false);
        passenger.setUserId((Long) session.getAttribute("user"));
        LambdaQueryWrapper<Passenger> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Passenger::getUserId, passenger.getUserId());
        if (passengerService.count(lambdaQueryWrapper) > 19) {
            return R.error("添加乘客数量已上限");
        }
        lambdaQueryWrapper.eq(Passenger::getIdCard, passenger.getIdCard());
        if(passengerService.count(lambdaQueryWrapper) > 0) {
            return R.error("该乘客已创建");
        }

        passengerService.save(passenger);
        return R.success("创建成功");
    }

    @GetMapping("/page")
    public R<Page<Passenger>> selectPage(HttpServletRequest request, Integer page, String name, String phone){
        log.info("selectPage..."+page);
        Page<Passenger> pageInfo=new Page<>(page,10);

        LambdaQueryWrapper<Passenger> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Passenger::getName,name);
        queryWrapper.like(phone!=null,Passenger::getPhone,phone);
        passengerService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping("/update")
    public R<String> update(HttpServletRequest request, @RequestBody Passenger passenger) {
        log.info("开始修改passenger={}", passenger);
        HttpSession session = request.getSession(false);
        passenger.setUserId((Long) session.getAttribute("user"));
        Passenger originPassenger = passengerService.getById(passenger.getId());
        LambdaQueryWrapper<Passenger> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!originPassenger.getIdCard().equals(passenger.getIdCard())) {
            lambdaQueryWrapper.eq(Passenger::getIdCard, passenger.getIdCard());
            lambdaQueryWrapper.eq(Passenger::getUserId, passenger.getUserId());
            if (passengerService.count(lambdaQueryWrapper) > 0) {
                return R.error("该乘客已存在");
            }
        }

        passengerService.updateById(passenger);
        return R.success("修改成功");
    }


    @PostMapping("/deleteByIds")
    public R<String> deleteByIds(HttpServletRequest request,@RequestBody List<Long> selectedIds){

        log.info("deleteByIds...");
        passengerService.removeByIds(selectedIds);

        return R.success("删除成功");
    }

    @DeleteMapping
    public R<String> deleteById(HttpServletRequest request,Long id){

        log.info("deleteById..."+id);
        passengerService.removeById(id);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Passenger>> list(HttpServletRequest request){
        log.info("list...");
        HttpSession session = request.getSession(false);
        Long userId=(Long) session.getAttribute("user");

        LambdaQueryWrapper<Passenger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Passenger::getUserId,userId);
        List<Passenger> passengers = passengerService.list(queryWrapper);
        return R.success(passengers);
    }

}
